/**
 * 系统公共工具方法集合
 */

import type {
  App,
  Plugin,
  Component,
  ComputedOptions,
  MethodOptions,
} from 'vue'

export const withInstall = <
  T extends Component<any, any, any, ComputedOptions, MethodOptions>,
>(
  component: T,
  alias?: string
) => {
  const comp = component as any
  comp.install = (app: App) => {
    app.component(comp.name || comp.displayName, component)
    if (alias) {
      app.config.globalProperties[alias] = component
    }
  }
  return component as T & Plugin
}

/** 前端唯一键 */
export function getGuid() {
  return 'xxxx_zxxx_yxxx_xxxx'.replace(/[xy]/g, function (c) {
    const r = (Math.random() * 16) | 0
    const v = c === 'x' ? r : (r & 0x3) | 0x8
    return v.toString(16)
  })
}
